import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router'
// import store from '@/store'
const service = axios.create({
  baseURL: '',
  timeout: 5000
})
service.interceptors.request.use(config => {
  const token = store.getters.token
  config.headers.Authorization = token
  return config
}, err => {
  return Promise.reject(err)
})
service.interceptors.response.use(response => {
  // console.log(response, 'response')
  // console.log(response.data.success, 'response.data.success')
  // console.log(response.config.isImg, 'response.config.isImg')

  if (response.status === 200) {
    return response.data
  } else {
    console.log('报错')
    Message.error(response.statusText)
    return Promise.reject(new Error(response.statusText))
  }
},
err => {
  // console.log(err, 'err')
  if (err.response.status === 401) {
    store.commit('user/removeToken')
    router.push('/login')
    Message.error('token超时 请重新登录')
  } else {
    Message.error(err.message)
  }
  return Promise.reject(err)
})
export default service
